<?php

namespace lib;

use app\facade\Redis;

class Token
{
    private $config = [
        'time_line' => 86400 * 14,
    ];

    public function set($user_id = 1, $prefix = 'user_')
    {
        $token = md5(uniqid() . time() . $user_id);
        return Redis::setex($prefix . $token, $this->config['time_line'], $user_id) ? $token : false;
    }

    public function get($token, $prefix = '')
    {
        $userId = Redis::get($prefix . $token);
        if ($userId) {
            Redis::expire($prefix . $token, $this->config['time_line']);
        }
        return $userId ?: false;
    }
}